package com.ljf.mysql.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ljf.framework.security.LjfSecurityManager;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;
import java.util.Objects;

public class FieldFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createBy", LjfSecurityManager.getUsername(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", LjfSecurityManager.getUsername(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object modifyTime = getFieldValByName("updateTime", metaObject);
        if (Objects.isNull(modifyTime)) {
            setFieldValByName("updateTime", new Date(), metaObject);
        }
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", LjfSecurityManager.getUsername(), metaObject);
    }

}
